﻿/**
 * 页面导航JS类
 *
 * 应用例子：
 * var plb = new PageListBar('plb', 343, 6, '?', 10);
 * document.write(plb);
 *
 * @author zhonggw<boy9732@msn.com>
 * @modified by Mandel Wu<wzjxp@msn.com>
 */


/**
 * 类初始化
 *
 * @param string name 对象名
 * @param integer pagecount 总页数
 * @param integer currpage 当前页号
 * @param string url 每个链接后的参数
 * @param integer listlength 显示的步长
 */
function PageListBar(name, pagecount, currpage, url, listlength, style, totalpost, showjump)
{
    this.name               = name;
    this.pagecount          = pagecount;                                //total page num
    this.currpage           = currpage;                                 //current page
    this.url                = url;                                      //link
    this.listlength         = listlength ? listlength : 3;				//number of page showed
	this.show_begin_end     = true; //是否显示“前一页”，“后一页”, Mandel Wu
	this.pagination_style   = style ? style : 0; //分页样式，0为旧样式，1为将所有分页都显示出来的样式, Mandel Wu 2006-12-26 13:46
	this.totalreply         = totalpost ? totalpost-1 : 0; //回帖总数，现在为给样式1所用
	this.showjump 	        = showjump ? showjump : 0; //回帖总数，现在为给样式1所用

	if (this.listlength < 0) {
		this.listlength = -1 * (this.listlength);
		this.show_begin_end = false;
	}

    if(this.pagecount <=0)
    {
        this.pagecount = 1;
    }
    if(this.currpage > this.pagecount)
    {
        this.currpage = this.pagecount;
    }
}

PageListBar.prototype.go = function(pagenum)
{
	if(pagenum == ''){
		return;
	}
	if(isNaN(pagenum)){
		pagenum = 1;
	}
	pagenum = parseInt(pagenum);
	if(pagenum >= this.pagecount){
		pagenum = this.pagecount;
	}
	if(pagenum <= 1){
		pagenum = 1;
	}
	if(pagenum == this.pagecount){
		var next_page = this.pagecount;
	}else{
		var next_page = pagenum+1;
	}
	if(pagenum == 1){
		var back_page = 1;
	}else{
		var back_page = pagenum-1;
	}
	$("#back").attr('href','javascript:'+ this.name + '.go(' + (back_page) + ');void(0);');
	$("#next").attr('href','javascript:'+ this.name + '.go(' + (next_page) + ');void(0);');
	$("#now_page").html(pagenum);
	$.ajax({
	   async: false, 
	   type: "GET",
	   url:	 this.url,
	   data: "page="+ pagenum,
	   dataType: "json",
	   success: function(msg){
			$("li[id^='page_']").each(function(o,i){
				$(i).removeClass();
			});
			$("#page_"+pagenum).addClass("current");
			$("#content_tjr").html(msg.ajax_html);
			$("#total_page").html(msg.total_page)
	   }
	}); 
}



PageListBar.prototype.goto = function()
{
    var currpage = prompt("请输入跳转到的页号",this.currpage);
    if(currpage)
    {
        if(currpage <= this.pagecount && currpage>=1)
        {
            this.go(currpage);
        }
    }
}
PageListBar.prototype.jump = function()
{
	var pagenum = document.getElementById('jump_input_'+this.name).value;
	var reg = /^[0-9]+$/;
	if(reg.test(pagenum)){
	    if(pagenum <= this.pagecount && pagenum>=1)
	    {
	        this.go(pagenum);
	    } else {
			alert("请填写1~"+this.pagecount+"之间的数字");
			document.getElementById('jump_input_'+this.name).select();
			document.getElementById('jump_input_'+this.name).focus();
	    }
	} else {
		alert("请填写1~"+this.pagecount+"之间的数字");
		document.getElementById('jump_input_'+this.name).select();
		document.getElementById('jump_input_'+this.name).focus();
	}
}
PageListBar.prototype.keycode = function(o)
{
    if (o.keyCode == 13)
    {
        this.jump();
    }
}

PageListBar.prototype.toString = function()
{
    var str = '', pStart = pEnd = 0;
	
	if (this.show_begin_end) {
		var str_fist = '<li><a href="javascript:' + this.name + '.go(' + (1) + ');void(0);" title="第一页" class="first"><span>&laquo;</span></a></li>';
	var str_last = '<li><a href="javascript:' + this.name + '.go(' + (this.pagecount) + ');void(0);" title="最后一页" class="last"><span>&raquo;</span></a></li>';
		var str_begin      = '<li><a id="back" href="javascript:' + this.name + '.go(' + ((this.currpage-1)<=1?1:(this.currpage-1)) + ');void(0);" title="上一页" class="prev"><span>上一页</span></a></li>';
		var str_end        = '<li><a id="next" href="javascript:' + this.name + '.go(' + ((this.currpage+1)>=this.pagecount?this.pagecount:(this.currpage+1)) + ');void(0);" title="下一页"  class="next"><span>下一页</span></a></li>';
		var str_body       = '';
		var str_begin_null = '<li><a href="#" title="上一页" class="prev"><span>上一页</span></a></li>';;
		var str_end_null   = '<li><a href="#" title="下一页"class="next"><span>下一页</span></a></li>';;
	} else {
		var str_fist = '';
		var str_last = '';
		var str_begin      = '';
		var str_end        = '';
		var str_body       = '';
		var str_begin_null = '';
		var str_end_null   = '';
	}

	if (this.pagination_style == 1)
	{
		str_begin = '<div id="page2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #000">共'+this.totalreply+'个回贴&nbsp;分'+this.pagecount+'页</span>&nbsp;&nbsp;&nbsp;&nbsp;';
		str_end = '</div>';
		str_body = '';
		var pageno_max_len = this.pagecount.toString().length;
		var str_page_label;
		var nbsp;

		for (var i=1; i<=this.pagecount; i++) {
			str_page_label = '';
			for (n=0; n<pageno_max_len - i.toString().length; n++) str_page_label += '0'; //统一长度
			str_page_label += i.toString();
			nbsp = i == this.pagecount ? '' : "&nbsp;";
			if(i == this.currpage) {
				str_body += '<span id="thispage2">&nbsp;第' + str_page_label + '页&nbsp;</span>'+nbsp;
			} else {
				str_body += '<a href="javascript:' + this.name + '.go(' + i + ');void(0);">&nbsp;第' + str_page_label + '页&nbsp;</a>'+nbsp;
			}
		}
		return '<div class="page_set"><ul class="page">'+str_fist+str_begin + str_body + "&nbsp;" + str_end+str_last+'</ul></div>';
	}
	//总页数小于4不采用...效果
	if(this.pagecount < 4)
	{	
		for(var i=1; i<=this.pagecount; i++)
		{
				str_body += '<li id="page_'+i+'"><a  href="javascript:' + this.name + '.go(' + i + ');void(0);"><span>' + i + '</span></a></li>';
		}

		//return str_begin + str_body + str_end;
		if (this.pagecount<2) {
			return "";		//return str_begin_null + str_body + str_end_null ;
		} else {
			return '<div class="page_set"><ul class="page">'+str_fist+str_begin + str_body + str_end+str_last+'</ul></div>' ;
		}
	}

	//预计显示长度小于实际总页数
	if((this.listlength * 2 + 1) >= this.pagecount - 2)
	{
		pStart = 2;
		pEnd = this.pagecount -1;
	}else{

		pStart = this.currpage - this.listlength;
		if(pStart < 2)
		{
			pStart = 2;
		}

		pEnd = this.currpage + this.listlength;
		if(pEnd > this.pagecount- 2)
		{
			pEnd = this.pagecount- 1;
		}
		var leftSpace  = this.listlength - (this.currpage - pStart);
		var rightSpace = this.listlength - (pEnd - this.currpage);

		//左余右填
		if(leftSpace > 0 && rightSpace <=0)
		{
			pEnd += leftSpace;
		}else{
			//右余左填
			if(rightSpace > 0 && leftSpace <=0)
			{
				pStart -= rightSpace;
			}
		}
	}
	for(var i=1; i<=this.pagecount; i++)
	{		if(i == 1){
				str_body += '<li class="current" id="page_'+i+'" ><a href="javascript:' + this.name + '.go(' + i + ');void(0);"><span>' + i + '</span></a></li>';
			}else{
				str_body += '<li id="page_'+i+'" ><a href="javascript:' + this.name + '.go(' + i + ');void(0);"><span>' + i + '</span></a></li>';
			}
		
	}
	return '<div class="page_set"><ul class="page">'+str_fist+str_begin + str_body + str_end+str_last+'</ul></div>';
}

